package com.zdkj.subway.support.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.common.security.util.ObjectUtils;
import com.zdkj.subway.support.dto.in.SwParamLineDTO;
import com.zdkj.subway.support.entity.SwParamLine;
import com.zdkj.subway.support.entity.SwParamReceipt;
import com.zdkj.subway.support.service.SwParamLineService;
import com.zdkj.subway.support.util.UploadDataListener;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;


/**
 * 线路设置表
 *
 * @author cui_com
 * @date 2020-01-08 17:13:42
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/swparamline")
@Api(value = "swparamline", tags = "线路设置表管理")
public class SwParamLineController {

    private final SwParamLineService swParamLineService;

    /**
     * 分页查询
     *
     * @param page        分页对象
     * @param swParamLine 线路设置表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public R getSwParamLinePage(Page page, SwParamLine swParamLine) {
        return R.ok(swParamLineService.page(page, Wrappers.query(swParamLine).orderByAsc("sort")));
    }


    /**
     * 通过id查询线路设置表
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Integer id) {
        return R.ok(swParamLineService.getById(id));
    }

    /**
     * 通过code查询线路名称
     * @param code
     * @return
     */
    @ApiOperation(value = "通过code查询线路名称", notes = "通过code查询")
    @GetMapping("/name/byCode/{code}")
    public R getLineNameByCode(@PathVariable("code") String code){return R.ok(swParamLineService.getNameByCode(code));}

    /**
     * 通过线路lineCode和站点code查询线路和站点名称
     * @param code
     * @return
     */
    @ApiOperation(value = "通过线路lineCode和站点code查询线路和站点名称", notes = "通过线路lineCode和站点code查询线路和站点名称")
    @GetMapping("/name/byLineCodeAndCode/{lineCode}/{code}")
    public R getLnameAndSnameByCode(@PathVariable("lineCode") String lineCode, @PathVariable(value = "code") String code){
        String name = "";
        Map<String, String> lnameAndSnameByCode = swParamLineService.getLnameAndSnameByCode(lineCode, code);
        if(ObjectUtil.isNotEmpty(lnameAndSnameByCode)){
            String lineName = lnameAndSnameByCode.get("lineName");
            String stationName = lnameAndSnameByCode.get("stationName");
            name = lineName +","+stationName;
            return R.ok(name);
        }
        return R.ok();
    }

    /**
     * 通过name查询线路code
     * @param name
     * @return
     */
    @ApiOperation(value = "通过name查询线路code", notes = "通过name查询线路code")
    @GetMapping("/name/byName/{name}")
    public R getCodeByName(@PathVariable("name") String name){return R.ok(swParamLineService.getCodeByName(name));}


    /**
     * 新增线路设置表
     *
     * @param swParamLine 线路设置表
     * @return R
     */
    @ApiOperation(value = "新增线路设置表", notes = "新增线路设置表")
    @SysLog("新增线路设置表")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('support_sw_param_line_add')")
    public R save(@RequestBody SwParamLineDTO swParamLine) {
        QueryWrapper<SwParamLine> namequeryWrapper=new QueryWrapper<>();
        namequeryWrapper.eq("name",swParamLine.getName());
        SwParamLine swParamLineName=swParamLineService.getOne(namequeryWrapper);
        if(ObjectUtil.isNotEmpty(swParamLineName)){
            return R.failed(swParamLineName,"线路名称"+swParamLineName.getName()+"已存在,线路名称不允许重复");
        }
        QueryWrapper<SwParamLine> codequeryWrapper=new QueryWrapper<>();
        codequeryWrapper.eq("code",swParamLine.getCode());
        SwParamLine swParamLinecode=swParamLineService.getOne(codequeryWrapper);
        if(ObjectUtil.isNotEmpty(swParamLinecode)){
            return R.failed(swParamLinecode,"线路编码"+swParamLinecode.getCode()+"已存在,线路编码不允许重复");
        }
        SwParamLine defaultObj = ObjectUtils.getDefaultObj(SwParamLine.class);
        BeanUtils.copyProperties(swParamLine, defaultObj);
        return R.ok(swParamLineService.save(defaultObj));
    }

    /**
     * 修改线路设置表
     *
     * @param swParamLine 线路设置表
     * @return R
     */
    @ApiOperation(value = "修改线路设置表", notes = "修改线路设置表")
    @SysLog("修改线路设置表")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('support_sw_param_line_edit')")
    public R updateById(@RequestBody SwParamLineDTO swParamLine) {
        QueryWrapper<SwParamLine> namequeryWrapper=new QueryWrapper<>();
        namequeryWrapper.eq("name",swParamLine.getName());
        SwParamLine swParamLineName=swParamLineService.getOne(namequeryWrapper);
        if(ObjectUtil.isNotEmpty(swParamLineName)&& !Objects.equals(swParamLine.getId(),swParamLineName.getId())){
            return R.failed(swParamLineName,"线路名称"+swParamLineName.getName()+"已存在,线路名称不允许重复");
        }
        QueryWrapper<SwParamLine> codequeryWrapper=new QueryWrapper<>();
        codequeryWrapper.eq("code",swParamLine.getCode());
        SwParamLine swParamLinecode=swParamLineService.getOne(codequeryWrapper);
        if(ObjectUtil.isNotEmpty(swParamLinecode)&& !Objects.equals(swParamLine.getId(),swParamLinecode.getId())){
            return R.failed(swParamLinecode,"线路编码"+swParamLinecode.getCode()+"已存在,线路编码不允许重复");
        }
        SwParamLine obj = new SwParamLine();
        BeanUtils.copyProperties(swParamLine, obj);
        return R.ok(swParamLineService.updateById(obj));
    }

    /**
     * 通过id删除线路设置表
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除线路设置表", notes = "通过id删除线路设置表")
    @SysLog("通过id删除线路设置表")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('support_sw_param_line_del')")
    public R removeById(@PathVariable Integer id) {
        return R.ok(swParamLineService.removeById(id));
    }

    /**
     * 线路字典接口
     *
     * @return R
     */
    @ApiOperation(value = "线路字典接口", notes = "线路字典接口")
    @GetMapping("/dict")
    public R getAll(@RequestParam(required = false) Boolean allLine) {
        List<SwParamLine> result = swParamLineService.getAll();
        if(allLine != null && allLine){
            SwParamLine allLineItem = new SwParamLine();
            allLineItem.setName("全线路");
            allLineItem.setCode("ALL");
            result.add(0, allLineItem);
        }
        return R.ok(result);
    }

    /**
     * 线路字典接口
     *
     * @return R
     */
    @ApiOperation(value = "线路字典接口", notes = "线路字典接口")
    @GetMapping("/dictAll")
    public R getRealAll(@RequestParam(required = false) Boolean allLine) {
        List<SwParamLine> result = swParamLineService.getRealAll();
        if(allLine != null && allLine){
            SwParamLine allLineItem = new SwParamLine();
            allLineItem.setName("全线路");
            allLineItem.setCode("ALL");
            result.add(0, allLineItem);
        }
        return R.ok(result);
    }

	 /**
     * 线路字典接口,区分一二期
     *
     * @return R
     */
    @ApiOperation(value = "线路字典接口,区分一二期", notes = "线路字典接口,区分一二期")
    @GetMapping("/dictsecondname")
    public R getLineBySeondname(@RequestParam(required = false) Boolean allLine) {
        List<SwParamLine> result = swParamLineService.getSecondNameLine();       
        return R.ok(result);
    }

    /**
     * 线路字典接口,区分一二期
     *
     * @return R
     */
    @ApiOperation(value = "线路字典接口,区分一二期", notes = "线路字典接口,区分一二期")
    @GetMapping("/getLineAll")
    public R getLineAll() {
        List<SwParamLine> result = swParamLineService.getLineAll();
        return R.ok(result);
    }

    /**
     * 线路字典接口,区分一二期 除了九号线和十四号线机场段
     *
     * @return R
     */
    @ApiOperation(value = "线路字典接口,区分一二期 除了九号线和十四号线机场段", notes = "线路字典接口,区分一二期 除了九号线和十四号线机场段")
    @GetMapping("/dictsecondnamecase")
    public R getLineBySeondnameCase(@RequestParam(required = false) Boolean allLine) {
        List<SwParamLine> result = swParamLineService.getSecondNameLineCase();
        return R.ok(result);
    }

    /**
     * 根据运营商查询线路
     *
     * @return R
     */
    @ApiOperation(value = "根据运营商查询线路", notes = "根据运营商查询线路")
    @GetMapping("/getLineByOperators")
    public R getLineByOperators(String operatorsId) {
        List<SwParamLine> result = swParamLineService.getLineByOperator(operatorsId);
        return R.ok(result);
    }

	 /**
     * 线路字典接口,区分一二期,返回ACC接口中的serviceID
     *
     * @return R
     */
    @ApiOperation(value = "线路字典接口,区分一二期,返回ACC接口中的serviceID", notes = "线路字典接口,区分一二期,返回ACC接口中的serviceID")
    @GetMapping("/dictoperator")
    public R getLineByOperator(@RequestParam(required = false) Boolean allLine) {
        List<SwParamLine> result = swParamLineService.getSecondNameLineByOperator();       
        return R.ok(result);
    }

	 /**
     * 线路字典接口,根据线路编号查询线路信息
     *
     * @return R
     */
    @ApiOperation(value = "线路字典接口,根据线路编号查询", notes = "线路字典接口,根据线路编号查询")
    @GetMapping("/dictbycode")
    public R getAll(@RequestParam(required = false) String lineCode) {
		if(lineCode!=null){
		    List<SwParamLine> result = swParamLineService.getLineByCondition(lineCode);
			return  R.ok(result);
		}
        List<SwParamLine> result = swParamLineService.getAll();
      
        return R.ok(result);
    }
    /**
     * 导入线路基础参数信息
     *
     * @param file
     * @return
     */
    @ApiOperation(value = "导入线路基础参数信息", notes = "导入线路基础参数信息")
    @SysLog("导入线路基础参数信息")
    @PostMapping("/import")
    public R importExcel(@RequestParam("file") MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), SwParamReceipt.class,
                    new UploadDataListener(swParamLineService, SwParamLine.class)).sheet().doRead();
            return R.ok("导入成功");
        } catch (IOException e) {
            log.info(e.getMessage());
            return R.ok("导入失败");
        }
    }
}
